
import * as menuData from '../../db/menuData'
var express = require('express');
var router = express.Router();
import { Request, Response } from 'express';

router.get('/api/menuList', (req: Request, res: Response) => {
  const { name } = req.query;
  menuData.selectMenu({ name }).then(data => {
    res.send({
      success: true,
      data,
    })
  }).catch(err => {
    res.status(500).send({
      data: {
        err,
      },
      errorCode: '500',
      errorMessage: '数据库错误：' + JSON.stringify(err),
      success: false,
    });
  })
});
router.post('/api/addMenu', (req: Request, res: Response) => {
  menuData.insertMenu(req.body).then(data => {
    res.send({
      success: true,
      data,
    })
  }).catch(err => {
    res.status(500).send({
      data: {
        err,
      },
      errorCode: '500',
      errorMessage: '数据库错误：' + JSON.stringify(err),
      success: false,
    });
  })
})
router.post('/api/editMenu', (req: Request, res: Response) => {
  menuData.updateMenu(req.body).then(data => {
    res.send({
      success: true,
      data,
    })
  }).catch(err => {
    res.status(500).send({
      data: {
        err,
      },
      errorCode: '500',
      errorMessage: '数据库错误：' + JSON.stringify(err),
      success: false,
    });
  })
});
router.get('/api/deleteMenu', (req: Request, res: Response) => {
  menuData.deleteMenu(Number(req.query.id)).then(data => {
    res.send({
      success: true,
      data,
    })
  }).catch(err => {
    res.status(500).send({
      data: {
        err,
      },
      errorCode: '500',
      errorMessage: '数据库错误：' + JSON.stringify(err),
      success: false,
    });
  })
});

module.exports = router;